
package biz.innovasoft.nodb.util;

/*
 * 
 * This class is not part of NoDB. This class is going to be part
 * of Prevayler 2.4.
 * 
 * You can download Prevayler-2.4 SNAPSHOT from 
 * 
 * http://github.com/jsampson/prevayler/tree/master
 * 
 */
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.prevayler.foundation.serialization.Serializer;
 
public class GZIPSerializer implements Serializer, Serializable {
 
	private static final long serialVersionUID = 9018990366842812010L;

	private final Serializer _delegate;
 
    private final int _bufferSize;
 
    public GZIPSerializer(Serializer delegate) {
        this(delegate, 512);
    }
 
    public GZIPSerializer(Serializer delegate, int gzipBufferSize) {
        _delegate = delegate;
        _bufferSize = gzipBufferSize;
    }
 
    public void writeObject(OutputStream stream, Object object) throws IOException {
        GZIPOutputStream gzip = new GZIPOutputStream(stream, _bufferSize);
        _delegate.writeObject(gzip, object);
        gzip.close();
    }
 
    public Object readObject(InputStream stream) throws IOException, ClassNotFoundException {
        GZIPInputStream gunzip = new GZIPInputStream(stream, _bufferSize);
        return _delegate.readObject(gunzip);
    }
 
}
